package com.csnf.dao;

import com.csnf.entity.Admin;
import com.csnf.util.JdbcUtil;

import java.sql.*;

/**
 * 对admin表的增删改查操作
 */
public class AdminDao {
    /**
     * 查询用户通过账号密码
     */
    public Admin getAdminByNamePwd(Admin admin) throws SQLException {
//        Connection conn = DriverManager.getConnection("jdbc:mysql:///hymalldb", "root", "root123");
//        PreparedStatement ps = conn.prepareStatement("select * from admin where admin_name=? and admin_password=?");
//        ps.setString(1,admin.getAdmin_name());
//        ps.setString(2,admin.getAdmin_password());
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from admin where admin_name=? and admin_password=?";
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,sql,admin.getAdmin_name(),admin.getAdmin_password());
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
//            admin.setAdmin_id(rs.getInt("admin_id"));
//            admin.setAdmin_name(rs.getString("admin_name"));
//            admin.setAdmin_nickname(rs.getString("admin_nickname"));
//            admin.setAdmin_password(rs.getString("admin_password"));
//            admin.setAdmin_profile_picture_src(rs.getString("admin_profile_picture_src"));
            try {
                admin = JdbcUtil.invokeObject(rs,Admin.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return admin;
        }
        return null;
    }
}
